define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            var evaluation_id = Config.evaluation_id
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'evaluation_field/index' + location.search,
                    add_url: 'evaluation_field/add?evaluation_id=' + evaluation_id,
                    edit_url: 'evaluation_field/edit?evaluation_id=' + evaluation_id,
                    del_url: 'evaluation_field/del',
                    multi_url: 'evaluation_field/multi',
                    import_url: 'evaluation_field/import',
                    table: 'evaluation_field',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                search: false,
                showToggle: false,
                showColumns: false,
                visible: false,
                showExport: false,
                commonSearch: false,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('ID'), operate: false},
                        {field: 'type.name', title: __('Type.name'), operate: false},
                        {field: 'project_name', title: __('Project_name'), operate: false},
                        {field: 'tools', title: __('Tools'), operate: false},
                        {
                            field: 'age_range', title: __('Age_range'), formatter: function (value, row, index) {
                                let age_range = "<div style='text-align:center;'>" + row.min_age;
                                if (row.min_age_unit == 1) {
                                    age_range += '个月';
                                } else {
                                    age_range += '年';
                                }
                                age_range += '-' + row.max_age;
                                if (row.max_age_unit == 1) {
                                    age_range += '个月';
                                } else {
                                    age_range += '年';
                                }
                                age_range += "</div>";
                                return age_range;
                            }, operate: false
                        },
                        {field: 'five_points', title: __('Five_points'), operate: false},
                        {field: 'three_points', title: __('Three_points'), operate: false},
                        {field: 'one_points', title: __('One_points'), operate: false},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [{
                                name: 'edit',
                                title: __('编辑'),
                                text: __('编辑'),
                                extend: 'data-toggle="tooltip"',
                                classname: 'btn btn-xs btn-success btn-editone',
                                url: 'evaluation_field/edit',
                                success: function (data, ret) {
                                    $('a.btn-refresh').trigger("click")
                                },
                            }, {
                                name: 'del',
                                title: __('删除'),
                                text: __('删除'),
                                classname: 'btn btn-xs btn-danger btn-magic btn-ajax',
                                confirm: __('Are you sure you want to delete this item?'),
                                url: 'evaluation_field/del',
                                success: function (data, ret) {
                                    $('a.btn-refresh').trigger("click")
                                },
                            },],
                            formatter: Table.api.formatter.buttons
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.add_group()
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        //新增数据
        add_group: function () {
            var add_index = 1;
            //新增
            $(document).on('click','.add_btn',function () {
                var html = '<div class="group_border">' +
                    '            <i class="fa fa-trash del_icon"></i>' +
                    '            <div class="form-group">' +
                    '                <label class="control-label col-xs-12 col-sm-2">'+__('Type')+':</label>' +
                    '                <div class="col-xs-10 col-sm-6">' +
                    '                    <input class="form-control selectpage" name="row[' + add_index + '][type]" type="text" data-source="type/index" data-params=\'{"custom[type]":"quality"}\'>' +
                    '                </div>' +
                    '            </div>' +
                    '            <div class="form-group">' +
                    '                <label class="control-label col-xs-12 col-sm-2">' + __('Project_name') + ':</label>' +
                    '                <div class="col-xs-12 col-sm-8">' +
                    '                    <input class="form-control" name="row[' + add_index + '][project_name]" type="text">' +
                    '                </div>' +
                    '            </div>' +
                    '            <div class="form-group">' +
                    '                <label class="control-label col-xs-12 col-sm-2">' + __('Age_range') + ':</label>' +
                    '                <div class="col-xs-2 col-sm-2">' +
                    '                    <input class="form-control" name="row[' + add_index + '][min_age]" type="text" value="">' +
                    '                </div>' +
                    '                <div class="col-xs-2 col-sm-2">' +
                    '                    <select data-rule="required" class="form-control selectpicker" name="row[' + add_index + '][min_age_unit]">' +
                    '                        <option value="1" selected>月</option>' +
                    '                        <option value="2">岁</option>' +
                    '                    </select>' +
                    '                </div>' +
                    '                <div style="float: left">-</div>' +
                    '                <div class="col-xs-2 col-sm-2">' +
                    '                    <input class="form-control" name="row[' + add_index + '][max_age]" type="text" value="">' +
                    '                </div>' +
                    '                <div class="col-xs-2 col-sm-2">' +
                    '                    <select data-rule="required" class="form-control selectpicker" name="row[' + add_index + '][max_age_unit]">' +
                    '                        <option value="1" selected>月</option>' +
                    '                        <option value="2">岁</option>' +
                    '                    </select>' +
                    '                </div>' +
                    '            </div>' +
                    '            <div class="form-group">' +
                    '                <label class="control-label col-xs-12 col-sm-2">' + __('Tools') + ':</label>' +
                    '                <div class="col-xs-12 col-sm-8">' +
                    '                    <input class="form-control" name="row[' + add_index + '][tools]" type="text">' +
                    '                </div>' +
                    '            </div>' +
                    '            <div class="form-group">' +
                    '                <label class="control-label col-xs-12 col-sm-2">' + __('Five_points') + ':</label>' +
                    '                <div class="col-xs-12 col-sm-8">' +
                    '                    <input class="form-control" name="row[' + add_index + '][five_points]" type="text">' +
                    '                </div>' +
                    '            </div>' +
                    '            <div class="form-group">' +
                    '                <label class="control-label col-xs-12 col-sm-2">' + __('Three_points') + ':</label>' +
                    '                <div class="col-xs-12 col-sm-8">' +
                    '                    <input class="form-control" name="row[' + add_index + '][three_points]" type="text">' +
                    '                </div>' +
                    '            </div>' +
                    '            <div class="form-group">' +
                    '                <label class="control-label col-xs-12 col-sm-2">' + __('One_points') + ':</label>' +
                    '                <div class="col-xs-12 col-sm-8">' +
                    '                    <input class="form-control" name="row[' + add_index + '][one_points]" type="text">' +
                    '                </div>' +
                    '            </div>' +
                    '            <input type="hidden" name="row[' + add_index + '][evaluation_id]" value="' + Config.evaluation_id + '">' +
                    '        </div>';
                $(".add_group").append(html)
                add_index++;
            })
            //删除
            $(document).on('click', '.del_icon', function () {
                $(this).parent().remove();
            })
        },
        api: {
            bindevent: function () {
                $("#addType").click(function () {
                    layer.open({
                        type: 2,
                        closeBtn: 1,
                        title: "类型设置",
                        area: ['80%', '80%'],
                        content: '/ssphp.php/type?type=quality',
                        // url: 'type/index',
                        success: function (layero, index) {
                        }
                    });
                })
                $(document).on("click", "form .add_btn", function (e, obj) {
                    Form.events.selectpage(obj);
                });
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
